package com.RHECmap.com;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem> {
	
	private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();		    
    private Context context;
    Path path;
   
    GeoPoint p_p = null;
    GeoPoint p_c = null;


	

	public ItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}
	
	public ItemizedOverlay(Drawable defaultMarker, Context context) {
		 this(defaultMarker);
		 this.context = context;
	}


		

/*	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "Touched", Toast.LENGTH_LONG).show();
		  return false;	
	}*/

	@Override
	protected OverlayItem createItem(int i) {
		return mapOverlays.get(i);
	}
	 
	 

	@Override
	public int size() {
		 return mapOverlays.size();
	}
  
	@Override
	public boolean onTap(int i) {
	
		OverlayItem item = mapOverlays.get(i);
		AlertDialog.Builder dialog = new AlertDialog.Builder(context);
		dialog.setTitle(item.getTitle());
		dialog.setMessage(item.getSnippet());
		dialog.setIcon(context.getResources().getDrawable(R.drawable.profile));
		
		String name_array[] = item.getTitle().split(":");
		if(name_array.length>0){
		String name = name_array[1].trim();
		Data.Device_ID = name;
		//Toast.makeText( context,Data.Device_ID,Toast.LENGTH_SHORT ).show();		
		}	
		
		String point_array[] = item.getSnippet().split("\n");
		if(point_array.length>0){
			String point = point_array[0].trim();
			point_array = point.split(":");
			if(point_array.length>0){
				point = point_array[1].trim();				
				point_array = point.split(",");
				if(point_array.length>0){					
					Data.lat =(point_array[0]);
					Data.lon =(point_array[1]);				
				}
			}
		}
			dialog.setNegativeButton("Show Profile" ,new OnClickListener() {			
			public void onClick(DialogInterface arg0, int arg1) {				
				Intent intent = new Intent();
				intent.setClass(context, ElephantProfile.class);	           
				context.startActivity(intent);
			}
		});	
		
		dialog.show();
		return true;				
	}
	
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		
		super.draw(canvas, mapView, shadow);
		
	}

	public void addOverlay(OverlayItem overlay) {
		mapOverlays.add(overlay);
		this.populate();
		
		
	}

	
	
/*	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		if (event.getAction() == 1) {                
            GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
           if(p != Data.gp1 && Data.t_count == 0){
            	Data.gp1 = p;
            	Data.t_count = 1;
            }
            else if(p != Data.gp2 && Data.t_count == 1){
            	Data.gp1 = p;
            	Data.t_count = 0;
            }
            else{
            	
            }
        //    Data.lat = p.getLatitudeE6() / 1E6 ;
        //    Data.lon = p.getLongitudeE6() /1E6 ;
          
          Toast.makeText(context,p.getLatitudeE6() / 1E6 + "," +p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show();

        }                            
      
	return false;
	}*/


	
		
}
